﻿<%@ Assembly Name="JM.JCY.SharePointComponent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3f76d78e78426e2" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SPObjNewsfeedUserControl.ascx.cs" Inherits="NCS.OCP.SharePointComponent.Newsfeed.SPObjNewsfeed.SPObjNewsfeedUserControl" %>


<script type="text/javascript">

    function newsfeedGetStaffId() {
        return $("#<%=HiddenFieldStaffId.ClientID %>").val();
    }

    function GetCommentLimitedLength() {
        return "100";
    }
</script>

<style type="text/css">
   
</style>

<script type="text/javascript" src="/_layouts/JM.JCY.Resource/scripts/OCPNewsfeed.js"></script>
<script type="text/javascript" src="/_layouts/JM.JCY.Resource/scripts/Global.js"></script>
<script type="text/javascript" src="/_layouts/JM.JCY.Resource/scripts/timeago.min.js"></script>
<script type="text/javascript" src="/_layouts/JM.JCY.Resource/scripts/jquery.tmpl.js"></script>

<script id="commentTmpl" type="text/x-jquery-tmpl">
<table class="commentPart" cellpadding="0" cellspacing="0" border="0">
 <tr>
   <td rowspan="3" style="width:60px;padding-left:6px;" valign="middle" >  <img src='${staff.imageUrl}'  class="newsfeedPostImg"/>   </td>
   <td><a class="newsfeedPostName_a" href="/PersonalSite/PersonalPages/PublicUser.aspx?StaffId=${staff.id}">${staff.name}</a></td>
 </tr>
 <tr>
 <td class="spContent">
 <span>{{html Content}}</span>
 {{if showMoreLink && showMoreLink=='true'}}
 <a id="viewMoreComment_${commentId}" moreContent="${moreContent}" class="moreFeedComment">更多</a>
 {{/if}}
 
 </td>
 </tr>
 <tr><td class="timeAgo">${CREATED_TIME}
 
 {{if staff.id==newsfeedGetStaffId()}}
 <span class="deleteComment" id="deleteComment_${commentId}">删除</span>
 {{/if}}
 </td></tr>


</table>

</script>

<script id="moreCommentTemplate" type="text/x-jquery-tmpl"> 
<div style="width:100%;background-color:#EEEEEE;padding-left:5px;padding-top:3px;padding-bottom:3px;margin-bottom:3px;cursor:hand;" id="moreComments">
        <div class="moreComments">
            <a>
                <span class="commentDesc">显示所有评论</span>(<span id="actualCountDiv">${actualCount}</span>)
            </a>
        </div>
    </div>

</script>

<script language="javascript" type="text/javascript">
    function displayComments(commentLimitedLength) {
        if ($("#HiddenObjectId").val() != getSharepointObjectId()) {
            $("#initStatus").val("0");
            $("#HiddenObjectId").val(getSharepointObjectId());
        }

        $.ajax({
            type: "get",
            url: newsfeedSvcUrl + "GetMoreCommentsBySPObj?objectId=" + getSharepointObjectId(),
            dataType: "jsonp",
            cache: false,
            error: function (a, b, c) { alert(c); },
            success: function (spobjectComments) {
                var obj = eval('(' + (spobjectComments) + ')');
                for (var o in obj) {
                    obj[o]["CREATED_TIME"] = $.timeago(obj[o]["CREATED_TIME"]);
                }
                $("#commentContainer").text("");

                $.each(obj, function (i, item) {
                    item.showMoreLink = 'false';
                    if (item.Content.length > parseInt(commentLimitedLength)) {
                        item.moreContent = item.Content.substring(commentLimitedLength);
                        item.showMoreLink = 'true';
                        item.Content = item.Content.substr(0, commentLimitedLength);
                    }
                });

                $("#commentTmpl").tmpl(obj).appendTo("#commentContainer");

                $(".moreFeedComment").click(function () {
                    var commentContainer = $(this).siblings("span");
                    var curComment = commentContainer.text();
                    var finalStr = curComment + $(this).attr("moreContent");
                    $(this).attr("moreContent", "");
                    commentContainer.text("");
                    commentContainer.text(finalStr);
                    $(this).hide();

                });


                if ($("#initStatus").val() == "0") {
                    var commentCount = $("#commentContainer .commentPart").length;
                    var lessThan = commentCount - 3;
                    $("#commentContainer .commentPart:lt(" + lessThan + ")").hide();
                    $("#slideStatus").val("up");
                    $("#initStatus").val("1");
                }

                if ($("#slideStatus").val() == "up") {
                    var commentCount = $("#commentContainer .commentPart").length;
                    var lessThan = commentCount - 3;
                    $("#commentContainer .commentPart:lt(" + lessThan + ")").hide();
                }

                if (obj.length > 3) {
                    $("#viewMoreComment").html("");
                    $("#moreCommentTemplate").tmpl(obj[0]).appendTo("#viewMoreComment");
                } else {
                    $("#viewMoreComment").html("");
                }
            }
        });
    }


    function slideUpAnimation(CommentContainer, index) {
        var childLength = CommentContainer.children(".commentPart").length;
        if (index < childLength - 1) {
            if (index < childLength - 3) {
                $(CommentContainer.children(".commentPart")[index]).slideUp(0, function () { slideUpAnimation(CommentContainer, index + 1); });
            }
            else {
                slideUpAnimation(CommentContainer, index + 1);
            }
        }
    }


    function slideDownAnimation(CommentContainer, index) {
        var childLength = CommentContainer.children(".commentPart").length;
        if (index >= 0) {
            if (index < childLength - 3) {
                $(CommentContainer.children(".commentPart")[index]).slideDown(0, function () { slideDownAnimation(CommentContainer, index - 1); });
                $("#slideStatus").val("down");
            }
            else {
                slideDownAnimation(CommentContainer, index - 1);
            }
        }
    }

    function InitalCtrlEvent(staffId, objId, commentLimitedLength) {

        $("#viewMoreComment").width($("#commentContainer").width() - 5);

        $("#moreComments").live("click", function () {
            var span = $(this).children(".commentDesc");
            if ($("#slideStatus").val() == "up") {
                slideDownAnimation($("#commentContainer"), $("#commentContainer").children(".commentPart").length - 3);
                $("#slideStatus").val("down");
                span.text("所有评论");
            }
            else if ($("#slideStatus").val() == "down") {
                slideUpAnimation($("#commentContainer"), 0);
                $("#slideStatus").val("up");
                span.text("显示所有评论");
            }
        });

        //DeleteComment
        $(".deleteComment").live("click", function () {
            var commentId = $(this).attr("id").split("_")[1];
            var container = $(this).parent().parent().parent().parent();
            $("#newsfeed-confirm").text("你真的要删除这个评论吗？");
            $("#newsfeed-confirm").dialog({
                resizable: false,
                height: 150,
                modal: true,
                buttons: {
                    "Ok": function () {
                        $.ajax({
                            type: "get",
                            dataType: "jsonp",
                            url: newsfeedSvcUrl + "DeleteComment?commentId=" + commentId,
                            cache: false,
                            success: function () {
                                container.hide(0, function () {
                                    container.remove();
                                    if ($("#commentContainer").children(".commentPart").length - 3 >= 0) {
                                        $("#actualCountDiv").text(parseInt($("#actualCountDiv").text()) - 1);
                                        if ($("#slideStatus").val() == "up") {

                                            var len = $("#commentContainer").children(".commentPart").length;
                                            var a = len - 3;
                                            $("#commentContainer").children(".commentPart:eq(" + a + ")").show(0);
                                        }
                                    }

                                    if ($("#commentContainer").children(".commentPart").length <= 3) {
                                        $("#viewMoreComment").html("");
                                    }
                                });
                            }
                        })
                        $(this).dialog("close");
                    },
                    "No": function () { $(this).dialog("close"); }
                }
            });

        });

        $("#txtComment").focus(function () {

            h = $(this).height();
            $(this).height(50);
            $(this).val("");
            $(this).addClass("commentAreaBorder");

        });


        $("#txtComment").blur(function () {
            if ($(this).val() == "") {
                $(this).height(h);
                $(this).val("请输入");
                $(this).removeClass("commentAreaBorder");
            }

        });


        $("#txtComment").keydown(function (event) {
            if (event.which == 13) {
                $("#btnComment").click();
                return false;
            }
        });


        $("#btnComment").click(function () {

            if ($("#txtComment").val().trim() != "" && $("#txtComment").val() != "请输入") {
                //insertComment(getSharepointObjectId(), staffId, appId, ctxId, shareOption, getMediaID(), getCurrentUrl(), function () {
                InsertComment(staffId, getSharepointObjectId(), function () {
                    $("#txtComment").val("");
                    $("#txtComment").height(h);
                    $("#txtComment").removeClass("commentAreaBorder");
                    displayComments(commentLimitedLength);
                });
            }
            else {
                showAlert('', '评论内容不能为空.');
            }
        });


    }

    function InsertComment(staffId, objId, callback) {
        var commentTxt = $("#txtComment").val();
        $.ajax({
            type: "get",
            dataType: "jsonp",
            contentType: "application/json; charset=utf-8",
            //InsertFeedPost(int staffId,string postContent,int objectId,string appType,int appId)
            url: newsfeedSvcUrl + "InsertCommentWithObjId?staffId=" + staffId + "&objId=" + objId + "&content=" + commentTxt,
            failure: function (msg) {
                alert(msg);
            },
            success: function (result) {
                $("#commentContainer").html("");
                callback();
                return;
            }
        });
    }
    //end
        

</script>

<script id="writeCommentTmpl" type="text/x-jquery-tmpl">


</script>

<script language="javascript" type="text/javascript">

    $(document).ready(function () {
        if ((window.location.pathname.toLowerCase().search("mohipedia/home.aspx") != 1) && (window.location.pathname.toLowerCase().search("mohipedia/viewmoremohipedia.aspx") != 1)) {
            setTimeout(function () {
                displayComments(GetCommentLimitedLength());
            }, 1000);

            setInterval(function () {
                displayComments(GetCommentLimitedLength());
            }, 30000);

            InitalCtrlEvent(newsfeedGetStaffId(), getSharepointObjectId(), GetCommentLimitedLength());
        }
    });

</script>

<div id="viewMoreComment">
</div>
<div id="commentContainer">
</div>

<input type="hidden" id="initStatus" value="0"/>
<input type="hidden" id="HiddenObjectId" value="0"/>
<input type="hidden" value="up" id="slideStatus"/>
  <table class="commentPart" cellpadding="0" cellspacing="0" border="0">
      <tr>
        <td class="tdCommentWrite" style="width:80%">
        <input type="text" id="txtComment" class="commentArea" value="请输入"/>       
        </td>
        <td style="text-align:right;padding-right:10px; padding-bottom:10px;padding-top:10px;">
        <img id="btnComment" src="_LAYOUTS/JM.JCY.Resource/images/newsfeedok.png" alt="" style="cursor:hand"/>
       </td>
      </tr>
   </table>
<asp:HiddenField ID="HiddenFieldStaffId" runat="server" />
